package com.jh3502.vivo.function.passchange;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.animation.Animation;
import android.widget.EditText;
import android.widget.TextView;

import com.jh3502.vivo.R;
import com.jh3502.vivo.base.BaseActivity;
import com.jh3502.vivo.utils.SpHelper;
import com.jh3502.vivo.view.SnackbarUtil;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import cxy.com.validate.IValidateResult;
import cxy.com.validate.Validate;
import cxy.com.validate.annotation.Index;
import cxy.com.validate.annotation.NotNull;
import cxy.com.validate.annotation.Password1;
import cxy.com.validate.annotation.Password2;

/**
 * @author harisucici on 2018/11/14 17:07
 * @email harisucici@gmail.com
 * @name todo:
 */
public class PassChangeActivity extends BaseActivity<PassChangePresenter.PassChangeView, PassChangePresenter> implements PassChangePresenter.PassChangeView {

    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.include_user_detail_name)
    TextView includeUserDetailName;
    @BindView(R.id.include_user_detail_com)
    TextView includeUserDetailCom;


    @Index(0)
    @NotNull(msg = "不能为空！")
    @BindView(R.id.pass_change_old)
    EditText passChangeOld;

    @Index(1)
    @NotNull(msg = "不能为空！")
    @Password1()
    @BindView(R.id.pass_change_new_1)
    EditText passChangeNew1;

    @Index(2)
    @NotNull(msg = "不能为空！")
    @Password2(msg = "两次密码不一致！！！")
    @BindView(R.id.pass_change_new_2)
    EditText passChangeNew2;

    @Override
    public PassChangePresenter initPresenter() {
        return new PassChangePresenter();
    }

    @Override
    protected int setViewId() {
        return R.layout.activity_pass_change;
    }

    @Override
    protected void initView() {
        toolbar.setTitle("");
        toolbarTitle.setText("修改密码");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        includeUserDetailName.setText(SpHelper.getInstance().getUserName());
        includeUserDetailCom.setText(SpHelper.getInstance().getDeptName());
        mPresenter.setPassChangeView(this);

    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_done, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                break;
            case R.id.action_done:
                Validate.check(this, new IValidateResult() {
                    @Override
                    public void onValidateSuccess() {
                        Map<String, Object> body = new HashMap<>(2);
                        body.put("oldPwd", passChangeOld.getText().toString().trim());
                        body.put("newPwd", passChangeNew1.getText().toString().trim());
                        mPresenter.toPassChange(body);

                    }

                    @Override
                    public void onValidateError(String s, EditText editText) {
                        showShortToast(s, SnackbarUtil.ALERT);
                    }

                    @Override
                    public Animation onValidateErrorAnno() {
                        return null;
                    }
                });
                break;
            default:
        }
        return true;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void doOnDestroy() {

    }

    @Override
    protected void doOnResume() {

    }

    @Override
    protected void doOnSaveInstanceState(Bundle savedInstanceState) {

    }


    @Override
    public void responseError(int code, String msg) {
        showShortToast(msg, SnackbarUtil.ALERT);

    }


    @Override
    public void changePwdSuccess() {
        showShortToast("修改成功");
    }


    @OnClick(R.id.iv0)
    public void onIv0Clicked() {
        if (passChangeNew1.getInputType() == 129) {
            passChangeNew1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        } else {
            passChangeNew1.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
        }

    }

    @OnClick(R.id.iv1)
    public void onIv1Clicked() {
        if (passChangeNew2.getInputType() == 129) {
            passChangeNew2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        } else {
            passChangeNew2.setInputType(InputType.TYPE_TEXT_VARIATION_PASSWORD | InputType.TYPE_CLASS_TEXT);
        }
    }
}
